/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.deployment.relatorio;

import br.com.deployment.daoImpl.AnexoDAOImpl;
import br.com.deployment.daoImpl.CategoryDAOImpl;
import br.com.deployment.daoImpl.IssuesDAOImpl;
import br.com.deployment.daoImpl.OptionsDAOImpl;
import br.com.deployment.daoImpl.ResponseDAOImpl;
import br.com.deployment.entities.Anexo;
import br.com.deployment.entities.Category;
import br.com.deployment.entities.Issues;
import br.com.deployment.entities.Options;
import br.com.deployment.entities.QuestionnaireResponse;
import br.com.deployment.entities.Response;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

/**
 *
 * @author ely
 */
@ManagedBean
@SessionScoped
public class ManagerRelQuestionnaireResponse implements Serializable{

    @EJB
    ResponseDAOImpl responseService;
    @EJB
    OptionsDAOImpl optionsService;
    @EJB
    CategoryDAOImpl categoryService;
    @EJB
    IssuesDAOImpl issuesService;
    @EJB
    AnexoDAOImpl  anexoService;
    
    private QuestionnaireResponse quesResponse;
    // Lista de Objetos
    private List<Response> responses;
    private List<Options>  options;
    private List<Category> categorys;
    private List<Issues>   issueses;
    private List<Response> lstReponses;
    private List<Anexo>    anexos;

    public ManagerRelQuestionnaireResponse() {
        quesResponse = new QuestionnaireResponse();
        
    }

 
    public void showQuestionnaireResponse(QuestionnaireResponse q) {
        this.quesResponse = q;
        this.quesResponse.setAnexo(anexoService.findQuestionnaireReponseAnexo(quesResponse));
        responses = new ArrayList<Response>();
        catchCategory();
//        categorys = categoryService.findCategoryQuestionnaire(this.quesResponse.getQuestionnaire());
        
//        for (Category c : categorys) {
//           responses.addAll(responseService.findResponseCategory(c,this.quesResponse));
//           
//           for(Response r : responses){
//               System.out.println(r.getIssues()); 
//               issueses = issuesService.findIssuesResponse(r);
//                for(Issues i: issueses){
////                     r.setIssues(i);
//                     options = optionsService.findOptionsResponse(r);
//                     r.setOptionses(options);
//                     anexos  = anexoService.findReponseAnexo(r);
//                     r.setAnexo(anexos);
//                     
//                }
//                
//           }
//        }
//        System.err.println("xxx: "+responses);
    }
    
    public void catchCategory(){
        categorys = categoryService.findCategoryQuestionnaire(this.quesResponse.getQuestionnaire());
        System.err.println("Size: "+categorys.size());
    }
    
    public List<Response> catchResponse(Category c){
        System.err.println("Response");
        if(c == null){
            return new ArrayList<Response>();
            
        }
        return responseService.findResponseCategory(c,this.quesResponse);
    }
    
    public Issues catchIssues(Response res){
        System.err.println("Issues");
        if(res == null){
            return null;
        }
        return issuesService.findIssuesResponseOne(res);
    }
    
    public List<Anexo> catchAnexo(Response r){
        System.err.println("Anexo");
        if(r == null){
            return null;
        }
        return anexoService.findReponseAnexo(r);
    }
    
    public List<Options> catchOptions(Response rs){
        if(rs == null){
            return new ArrayList<Options>();
        }
        return  optionsService.findOptionsResponse(rs);
    }
    
    

    public void viewQuestionnaireResponse(QuestionnaireResponse q) {
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            this.quesResponse = q;
            showQuestionnaireResponse(this.quesResponse);
            context.getExternalContext().redirect("viewResponse.xhtml");
        } catch (IOException ex) {
            Logger.getLogger(ManagerRelQuestionnaireResponse.class
                    .getName()).log(Level.SEVERE, null, ex);
        }
    }

    public List<Response> getResponses() {
        return responses;
    }

    public void setResponses(List<Response> responses) {
        this.responses = responses;
    }

    public QuestionnaireResponse getQuesResponse() {
        return quesResponse;
    }

    public void setQuesResponse(QuestionnaireResponse quesResponse) {
        this.quesResponse = quesResponse;
    }

    public List<Category> getCategorys() {
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    public List<Options> getOptions() {
        return options;
    }

    public void setOptions(List<Options> options) {
        this.options = options;
    }

    public List<Response> getLstReponses() {
        return lstReponses;
    }

    public void setLstReponses(List<Response> lstReponses) {
        this.lstReponses = lstReponses;
    }
}
